#include "VNCClient.h"
#include "LockImp.h"

CVNCClient::CVNCClient()
{
	m_lpMgr = 0;
	m_lpSocket = 0;
	m_uKey = 0;
	m_szClientName = 0;
	m_lpThread = 0;
}

CVNCClient::~CVNCClient()
{
	UnInit();
}

bool CVNCClient::Init(BaseClientMgr* mgr,BaseSocket *socket,bool auth,bool shared,unsigned short newid)
{
	if( mgr == NULL || socket == NULL )
		return false;


	m_lpMgr = mgr;
	m_lpSocket = socket;
	m_uKey = newid;

	char* name = m_lpSocket->GetPeerName();
	if( name != 0 )
		m_szClientName = strdup(name);
	else
		m_szClientName = strdup("<unknown>");


	m_lpThread = new CVNCClientThread();
	if( m_lpThread == 0 )
		return false;

	return ((CVNCClientThread*)m_lpThread)->Init(this,socket,auth,shared);
}

void CVNCClient::UnInit()
{
	if( m_szClientName != 0 )
	{
		free(m_szClientName);
		m_szClientName = 0;
	}

	if( m_lpThread )
	{
		delete m_lpThread;
		m_lpThread = 0;
	}	
}

unsigned short CVNCClient::Destroy()
{
	unsigned short uKey = m_uKey;
	delete this;
	return uKey;
}

unsigned short CVNCClient::GetKey()
{
	return m_uKey;
}

void CVNCClient::OnServerEvent()
{
	
}

void CVNCClient::Kill()
{
	if( m_lpSocket )
	{
		m_lpSocket->Close();
	}
}

void CVNCClient::SetBuffer(BaseBuffer *buffer)
{
	if( buffer == 0 )
		return;

	m_cEncodeMgr.SetBuffer(buffer);
}

void CVNCClient::UpdateMouse()
{
	
}